import numpy as np
import matplotlib.pyplot as plt

# 定义函数
def f(x):
    return 3 * np.cbrt(x**2) - 2*x

# 定义区间
a, b = -1, 0.5

# 计算关键点
critical_points = [a, b, 0]  # 包括端点和导数不存在点

# 计算函数值
func_values = [f(point) for point in critical_points]

print("关键点及函数值:")
for point, value in zip(critical_points, func_values):
    print(f"f({point}) = {value}")

# 寻找最值
max_index = np.argmax(func_values)
min_index = np.argmin(func_values)

max_point = critical_points[max_index]
max_value = func_values[max_index]
min_point = critical_points[min_index]
min_value = func_values[min_index]

print(f"函数在区间 [{a}, {b}] 上的最大值为 {max_value}，在 x = {max_point} 处取得")
print(f"函数在区间 [{a}, {b}] 上的最小值为 {min_value}，在 x = {min_point} 处取得")

# 可视化
x_vals = np.linspace(a, b, 400)
y_vals = f(x_vals)

plt.figure(figsize=(10, 6))
plt.plot(x_vals, y_vals, 'b-', linewidth=2, label='f(x) = 3∛(x²) - 2x')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.grid(True, alpha=0.3)

# 标记关键点
for point in critical_points:
    y_val = f(point)
    plt.scatter(point, y_val, color='red', s=100, zorder=5)

plt.legend()
plt.show()